/*
 * {EasyGank}  Copyright (C) {2015}  {CaMnter}
 *
 * This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
 * This is free software, and you are welcome to redistribute it
 * under certain conditions; type `show c' for details.
 *
 * The hypothetical commands `show w' and `show c' should show the appropriate
 * parts of the General Public License.  Of course, your program's commands
 * might be different; for a GUI interface, you would use an "about box".
 *
 * You should also get your employer (if you work as a programmer) or school,
 * if any, to sign a "copyright disclaimer" for the program, if necessary.
 * For more information on this, and how to apply and follow the GNU GPL, see
 * <http://www.gnu.org/licenses/>.
 *
 * The GNU General Public License does not permit incorporating your program
 * into proprietary programs.  If your program is a subroutine library, you
 * may consider it more useful to permit linking proprietary applications with
 * the library.  If this is what you want to do, use the GNU Lesser General
 * Public License instead of this License.  But first, please read
 * <http://www.gnu.org/philosophy/why-not-lgpl.html>.
 */

package com.dhwl.hsh.merchant.ui.activity;


import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.dhwl.hsh.lib.utils.SystemBarTintManager;
import com.dhwl.hsh.lib.utils.SystemTool;
import com.dhwl.hsh.lib.utils.ToastUtils;
import com.dhwl.hsh.merchant.R;
import com.dhwl.hsh.merchant.ui.dialog.LoadingDialogFragment;
import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;

/**
 * Description：
 * Created by：CaMnter
 * Time：2016-01-02 15:02
 */
public abstract class BaseActivity extends FragmentActivity {


    /**
     * topbar 左侧内容
     */
    ImageView topbarLeftImg;

    /**
     * topbar title
     */
    TextView topbarTitleTv;

    /**
     * topbar 右侧内容
     */
    TextView topbarRightTv;

    public SystemBarTintManager mTintManager;

    private FragmentTransaction ft;

    private DialogFragment newFragment;

    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);//
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
            //            getWindow().setNavigationBarColor(Color.TRANSPARENT);
        }
        //System Bar由Status Bar<位于顶部>和Navigation Bar<位于底部>组成。
        mTintManager = new SystemBarTintManager(this);
//        mTintManager.setTintColor(Color.parseColor("#99000FF"));
        mTintManager.setStatusBarTintColor(getResources().getColor(R.color.color_32365d));
        mTintManager.setStatusBarTintEnabled(true);
        mTintManager.setNavigationBarTintEnabled(true);

        this.setContentView(this.getLayoutId());
        ButterKnife.bind(this);


        topbarLeftImg = ButterKnife.findById(this, R.id.topbar_left_img);
        topbarTitleTv = ButterKnife.findById(this, R.id.topbar_title_tv);
        topbarRightTv = ButterKnife.findById(this, R.id.topbar_right_tv);
        if (null != topbarTitleTv) {
            topbarTitleTv.setFocusable(true);
            topbarTitleTv.requestFocus();
        }
        if (null != topbarLeftImg) {
            topbarLeftImg.setVisibility(View.VISIBLE);
            topbarLeftImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SystemTool.hideSysKeyBoard(BaseActivity.this);
                    finish();
                }
            });
        }

        this.initViews(savedInstanceState);

        ft = getSupportFragmentManager().beginTransaction();
        fragmentManager = getSupportFragmentManager();

        this.initData();

    }


    /**
     * Fill in layout id
     *
     * @return layout id
     */
    protected abstract int getLayoutId();


    /**
     * Find the view by id
     *
     * @param id  id
     * @param <V> V
     * @return V
     */
    @SuppressWarnings("unchecked")
    protected <V extends View> V findView(int id) {
        return (V) this.findViewById(id);
    }


    /**
     * Initialize the view in the layout
     *
     * @param savedInstanceState savedInstanceState
     */
    protected abstract void initViews(Bundle savedInstanceState);

    /**
     * Initialize the Activity data
     */
    protected abstract void initData();


    @Override
    protected void onResume() {
        super.onResume();
        //防止系统设置字体放大
        Resources resource = getResources();
        Configuration c = resource.getConfiguration();
        c.fontScale = 1.0f;
        resource.updateConfiguration(c, resource.getDisplayMetrics());

        if (!(this instanceof MainActivity)) {
            MobclickAgent.onPageStart(this.getClass().getSimpleName());
        }
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (!(this instanceof MainActivity)) {
            MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        }
        MobclickAgent.onPause(this);
    }

    /**
     * 设置topbar 左侧内容
     *
     * @param resId 通过string.xml中的资源来设置按钮的内容
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setRightBtnContent(int resId) {
        topbarRightTv.setVisibility(View.VISIBLE);
        topbarRightTv.setText(resId);

        return topbarRightTv;
    }

    /**
     * 设置topbar title
     *
     * @param resId 通过string.xml资源来设置标题
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setTitleContent(int resId) {
        return setTitleContent(getString(resId));
    }

    /**
     * 设置topbar title
     *
     * @param title 通过string.xml资源来设置标题
     * @return
     * @see [类、类#方法、类#成员]
     */
    protected View setTitleContent(String title) {
        topbarTitleTv.setText(title);
        return topbarTitleTv;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    /*********
     * Toast *
     *********/

    public void showToast(String msg) {
        this.showToast(msg, Toast.LENGTH_SHORT);
    }


    public void showToast(String msg, int duration) {
        if (msg == null) return;
        if (duration == Toast.LENGTH_SHORT || duration == Toast.LENGTH_LONG) {
            ToastUtils.show(this, msg, duration);
        } else {
            ToastUtils.show(this, msg, ToastUtils.LENGTH_SHORT);
        }
    }


    public void showToast(int resId) {
        this.showToast(resId, Toast.LENGTH_SHORT);
    }


    public void showToast(int resId, int duration) {
        if (duration == Toast.LENGTH_SHORT || duration == Toast.LENGTH_LONG) {
            ToastUtils.show(this, resId, duration);
        } else {
            ToastUtils.show(this, resId, ToastUtils.LENGTH_SHORT);
        }
    }

    /**
     * 显示加载动画
     */
    public synchronized void showWaitingDialog() {
        Fragment loadFragment = fragmentManager.findFragmentByTag("load_dialog");
        if (loadFragment != null) {
            ft.remove(loadFragment);
        }
        ft.addToBackStack(null);

        // Create and show the dialog.
        if (null == newFragment) {
            newFragment = LoadingDialogFragment.newInstance();
        }
        if (!newFragment.isVisible())
            newFragment.show(ft, "load_dialog");
    }

    /**
     * 隐藏加载动画
     */
    public void hideWaitingDialog() {
        if (null != newFragment) {
            newFragment.dismissAllowingStateLoss();
        }
    }


}
